#include <stdio.h>
#include <stdbool.h>

//验证该值是否为整数
long get_long(void);

//验证范围的上下限是否有效
bool bad_limits(long begin, long end, long low, long high);

//计算a~b的之间的整数平方和
double sum_squares(long a, long b);

int main(void)
{
    const long MIN = -10000000L;
    const long MAX = +10000000L;
    long start, end;
    double answer;

    printf("This program computes the sum of the squares of integers in a range.\n");
    printf("The lower bound should not be less than -10000000 and the upper bound should not be more than +10000000.\n");
    printf("Enter the limits (enter 0 for both limits to quit): \n");
    
    printf("lower limit: ");
    start = get_long();
    printf("upper limit: ");
    end = get_long();

    while (start != 0 || end != 0)
    {
        if (bad_limits(start, end, MIN, MAX))
            printf("Please try again.\n");
        else
        {
            answer = sum_squares(start, end);
            printf("The sum of the squares of the integers form %ld to %ld is %g.\n", start, end, answer);
        }
        printf("Enter the limits (enter 0 for both limits to quit): \n");

        printf("lower limit: ");
        start = get_long();
        printf("upper limit: ");
        end = get_long();
    }
    printf("Done.\n");

    return 0;
}

double sum_squares(long a, long b)
{
    double sum = 0.0;

    while (a <= b)
    {
        sum = sum + (double)a * (double)a;
        a++;
    }
    
    return sum;
}

bool bad_limits(long begin, long end, long low, long high)
{
    bool not_good = false;
    if (begin > end)
    {
        printf("%ld isn't smaller than %ld.\n", begin, end);
        not_good = true;
    }

    if (begin < low || end < low)
    {
        printf("Values must be %ld or greater.\n");
        not_good = true;
    }

    if (begin > high || end > high)
    {
        printf("Values must be %d or less.\n");
        not_good = true;
    }

    return not_good;
}

long get_long(void)
{
    long input;
    char ch;

    while ((scanf("%ld", &input)) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an integer.\nPlease enter an interger value, such as 25, -178, or 3: ");
    }

    return input;
}